package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.OrderDto;
import io.geekidea.boot.shop.entity.Order;
import io.geekidea.boot.shop.mapper.OrderMapper;
import io.geekidea.boot.shop.query.app.AppOrderQuery;
import io.geekidea.boot.shop.query.web.OrderQuery;
import io.geekidea.boot.shop.service.base.api.OrderService;
import io.geekidea.boot.shop.vo.app.AppOrderVo;
import io.geekidea.boot.shop.vo.web.OrderVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addOrder(OrderDto dto) {
        Order order = new Order();
        BeanUtils.copyProperties(dto, order);
        return save(order);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateOrder(OrderDto dto) {
        Integer id = dto.getId();
        Order order = getById(id);
        if (order == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, order);
        return updateById(order);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteOrder(Integer id) {
        return removeById(id);
    }

    @Override
    public OrderVo getOrderById(Integer id) {
        return orderMapper.getOrderById(id);
    }

    @Override
    public Paging<OrderVo> getOrderPage(OrderQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<OrderVo> list = orderMapper.getOrderPage(query);
        Paging<OrderVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppOrderVo getAppOrderById(Integer id) {
        return orderMapper.getAppOrderById(id);
    }

    @Override
    public Paging<AppOrderVo> getAppOrderPage(AppOrderQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppOrderVo> list = orderMapper.getAppOrderPage(query);
        Paging<AppOrderVo> paging = new Paging<>(list);
        return paging;
    }

}
